// pages/login/login.ts
const app = getApp()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        userinfo: {
            username: "",
            password: ""
        },
        avatarUrl: defaultAvatarUrl,
    },
    changeinput(e: any) {
        let userinfo = this.data.userinfo
        if (e.currentTarget.dataset.type == "name") {
            userinfo.username = e.detail.value
        } else if (e.currentTarget.dataset.type == "pwd") {
            userinfo.password = e.detail.value
        }
        this.setData({ userinfo })
    },
    onChooseAvatar(e:any) {
        const { avatarUrl } = e.detail 
        this.setData({
          avatarUrl,
        })
      },
    /**
     * 处理输入事件，更新userinfo对象
     */
    handleInputChange(e: WechatMiniprogram.Input) {
        const field = e.currentTarget.dataset.field;
        const value = e.detail.value;
        this.setData({
            [`userinfo.${field}`]: value
        });
    },
    /**
     * 登录方法
     */
    login() {
        console.log("Login method called")
        console.log("Global islogin flag:", app.globalData.islogin)
        console.log("Userinfo before request:", this.data.userinfo)

        wx.request({
            url: 'http://localhost:9090/login', // 替换为你的实际后端地址
            method: 'POST',
            data: {
                vuUserId: this.data.userinfo.username,
                vuPassword: this.data.userinfo.password
            },
            success(res: any) {
                if (res.data.code == 200) {
                    console.log(res.data.code)
                    wx.switchTab({ url: '../my/my' });
                } else {
                    wx.showToast({
                        title: '该用户已被禁用',
                        icon: 'error',
                        image: '../../images/0zg2712000eigyp174294.jpeg'
                    });
                }
            },
            fail() {
                wx.showToast({
                    title: '请求失败，请稍后重试',
                    icon: 'none'
                });
            }
        });
    },
    wxlogin() {
        const that = this; // 将页面上下文保存到that变量
        console.log("wxlogin method called")
        console.log("Userinfo before wx.login:", this.data.userinfo)
        
        wx.login({
            success(res) {
                console.log("wx.login success, code:", res.code)
                // 使用 that 引用页面的 this 上下文，确保能正确访问 data
                wx.request({
                    url: 'http://192.168.86.1:9090/wxlogin', // 替换为你的实际后端地址
                    method: 'GET',
                    data: {
                        // id: that.data.userinfo.username,
                        // name: that.data.userinfo.username,
                        // password: that.data.userinfo.password,
                        code: res.code
                    },
                    success(request) {
                        console.log("wx.request success, response:", request)
                    },
                    fail(error) {
                        console.error("wx.request failed, error:", error)
                        wx.showToast({
                            title: '请求失败，请稍后重试',
                            icon: 'none'
                        });
                    }
                })
            },
            fail(err) {
                console.error("wx.login failed, error:", err)
                wx.showToast({
                    title: '微信登录失败',
                    icon: 'none'
                });
            }
        })
    },
     // 跳转到 index 页面的方法
     index() {
        wx.navigateTo({
            url: '../index/index'
        });
    },
    // 返回上一页的方法
    Toback() {
        wx.navigateBack();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        console.log("Page loaded");
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        console.log("Page ready");
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log("Page shown");
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        console.log("Page hidden");
    },

    /**
     *监听页面卸载
     */
    onUnload() {
        console.log("Page unloaded");
    },

    /**
     *页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        console.log("Page pull down refresh");
    },

    /**
     *页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log("Page reach bottom");
    },

    /**
     *用户点击右上角分享
     */
    onShareAppMessage() {
        console.log("Page share app message");
    }
})